package com.project.controller.web;

import com.project.common.utils.MyPageHelper;
import com.project.common.validator.ValidatorUtils;
import com.project.common.validator.group.AddGroup;
import com.project.entity.*;
import com.project.myBeans.R;
import com.project.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import com.project.myBeans.ExerciseDetail;

import java.util.ArrayList;
import java.util.List;


@EnableAutoConfiguration
@RestController
@RequestMapping(value = "/exam")
@Api(description = "考试接口",value = "exam")
public class ExamController
{
    @Autowired
    ExamService examService;

    @Autowired
    private ComposePaperService composePaperService;

    @Autowired
    private ExerciseInfoService exerciseInfoService;

    @Autowired
    private ChoiceService choiceService;

    @Autowired
    private TrueFalseService trueFalseService;

    @Autowired
    private CompletionService completionService;

    @Autowired
    private CompAnswerService compAnswerService;

    @Autowired
    private ShortAnswerService shortAnswerService;

    @Autowired
    private ProgrammingService programmingService;

    @Autowired
    private AnswerRecordService answerRecordService;

    @Autowired
    private ExerciseRecordService exerciseRecordService;

    @Autowired
    private ExerciseService exerciseService;

    @Autowired
    private ExamRecordService examRecordService;

    /**
     * fyk 2018/3/27
     * @return
     */
    @ApiOperation("通过课程ID查找考试")
    @PostMapping("/findByCourseid/{courseid}")
    public R findByCourseid(@PathVariable Integer courseid)
    {
        try
        {
//            MyPageHelper.setPageParam(exam);
            List<Exam> list = examService.findByCourseid(courseid);
            return R.ok().put("data",list).put("total", MyPageHelper.getTotal(list));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("通过学生id查找考试")
    @GetMapping("/findByStudentid/{studentid}")
    public R findByStudentid(@PathVariable Integer studentid)
    {
        try
        {
            return R.ok().put("data",examService.findByStudentid(studentid));
        }catch(Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * wnt 2018/5/10
     * @return
     */
    @ApiOperation("添加考试")
    @PostMapping("/add")
    public R add(@RequestBody Exam exam)
    {
        try
        {
            if(examService.add(exam)>0)
            {
                return R.ok();
            }else
            {
                return R.error("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
    /**
     * Tate 2018/4/9
     * @param id
     * @return
     */
    @ApiOperation("通过id删除考试")
    @GetMapping("/remove/{id}")
    public R removeOne(@PathVariable Integer id){
        examService.remove(id);
        return R.ok();
    }

    /**
     * Tate 2018/5/23
     *
     * @return
     */
    @ApiOperation("通过授课ID查找考试")
    @PostMapping("/findBycourseteachid/{courseteachid}")
    public R findBycourseteachid(@PathVariable Integer courseteachid) {
        try {
            List<Exam> list = examService.findBycourseteachid(courseteachid);
            return R.ok().put("data", list).put("total", MyPageHelper.getTotal(list));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }



    /**
     * Tate 2018/5/23
     *
     * @param exam
     * @return
     */
    @ApiOperation("修改考试信息")
    @PostMapping("/modify")
    public R modifyexam(@RequestBody Exam exam) {
        try {
            ValidatorUtils.validateEntity(exam, AddGroup.class);
            examService.modify(exam);
            return R.ok("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * wqd 2018/6/2
     * @param id
     * @return
     */
    @ApiOperation("通过考试ID查找考试")
    @PostMapping("/findById/{id}")
    public R findById(@PathVariable Integer id)
    {
        try
        {
            Exam exam = examService.findById(id);
            return R.ok().put("data",exam);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查找失败");
        }
    }

    @ApiOperation("通过考试ID查找所有题目")
    @PostMapping("/findExerciseDetailByexamid/{id}")
    public R findExerciseDetailByexamid(@PathVariable Integer id)
    {
        try
        {
            Exam exam = examService.findById(id);
            Integer exampaperid = exam.getExampaperid();
            List<ComposePaper> composePapers=composePaperService.findByPaperId(exampaperid);
            List<ExerciseDetail> exerciseDetailList = new ArrayList<>();
            for(ComposePaper composePaper:composePapers) {
                ExerciseDetail exerciseDetail = new ExerciseDetail();
                exerciseDetail.setComposePaper(composePaper);
                exerciseDetail.setExamRecord(new ExamRecord());
                exerciseDetail.setExamBlankrecords(new ArrayList<>());
                exerciseDetail.setExerciseInfo(exerciseInfoService.findByPrimaryKey(composePaper.getExerinfoid()));
                if(exerciseInfoService.findByPrimaryKey(composePaper.getExerinfoid())!=null){
                    switch (exerciseInfoService.findByPrimaryKey(composePaper.getExerinfoid()).getType()) {
                        case 1: //单选
                        case 2: //多选
                            Choice choice = choiceService.findByExerciseInfoId(composePaper.getExerinfoid());
                            choice.setAnswer(null);
                            choice.setComment(null);
                            exerciseDetail.setChoice(choice);
                            break;
                        case 3:
                            TrueFalse truefalse = trueFalseService.findByExerciseInfoId(composePaper.getExerinfoid());
                            truefalse.setAnswer(null);
                            truefalse.setComment(null);
                            exerciseDetail.setTrueFalse(truefalse);
                            break;
                        case 4:
                            Completion completion = completionService.findByExerciseInfoId(composePaper.getExerinfoid());
                            String[] strs = compAnswerService.findByCompidOrderByIndex(completion.getId());
                            completion.setBlankCount(strs.length);
                            completion.setAnswerArray(null);
                            completion.setComment(null);
                            exerciseDetail.setCompletion(completion);
                            break;
                        case 5:
                            ShortAnswer shortAnswer = shortAnswerService.findByExcerciseInfoId(composePaper.getExerinfoid());
                            shortAnswer.setAnswer(null);
                            exerciseDetail.setShortAnswer(shortAnswer);
                            break;
                        case 6:
                            Programming programming = programmingService.findByExerciseInfoId(composePaper.getExerinfoid());
                            programming.setAnswer(null);
                            exerciseDetail.setProgramming(programming);
                            break;
                    }
                }
                exerciseDetailList.add(exerciseDetail);
            }
            return R.ok().put("data",exerciseDetailList).put("total", MyPageHelper.getTotal(composePapers));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查找失败");
        }
    }


    /**
     * @Description: 根据试卷id批改试卷
     * @param: examid
     * @return: R
     * @author: TateBrown
     * @Date: 2018/6/17
     */
//     @ApiOperation("根据试卷id批改试卷")
//     @GetMapping("/CheckanswerByExamid/{examid}")
//   public R CheckanswerByExamid(@PathVariable Integer examid){
//        try{
//         Exam exam = examService.findById(examid);
//         List<ComposePaper> composePapers=composePaperService.findByPaperId(exam.getExampaperid());
//         for(ComposePaper composePaper:composePapers){
////             List<AnswerRecord> answerRecords=answerRecordService.findbytwo(examid,composePaper.getExerinfoid());
////             ExerciseInfo exerciseInfo=exerciseInfoService.findByPrimaryKey(composePaper.getExerinfoid());
////             for(AnswerRecord answerRecord:answerRecords){
////                   Integer type=exerciseInfo.getType();
////                 ExamRecord examRecord=examRecordService.findbyAnswerrecordid(answerRecord.getId());
//                   switch (type){
//                       case 1://单选
//                       case 2://多选
//                               Choice choice=choiceService.findByExerciseInfoId(exerciseInfo.getId());
//                               if(examRecord.getAnswer().trim().equals(choice.getAnswer())){
//                                   examRecord.setIstrue(2);
//                                   examRecord.setScore(composePaper.getPoints());
//                                   examRecordService.modifysc(examRecord);
//                               }else{
//                                   examRecord.setIstrue(3);
//                                   examRecord.setScore((float)0);
//                                   examRecordService.modifysc(examRecord);
//                               }
//                               break;
//                       case 3:
//                           TrueFalse truefalse = trueFalseService.findByExerciseInfoId(composePaper.getExerinfoid());
//                           if(examRecord.getAnswer().trim().equals(truefalse.getAnswer().toString())){
//                               examRecord.setIstrue(2);
//                               examRecord.setScore(composePaper.getPoints());
//                               examRecordService.modifysc(examRecord);
//                           }else{
//                               examRecord.setIstrue(3);
//                               examRecord.setScore((float)0);
//                               examRecordService.modifysc(examRecord);
//                           }
//                           break;
//                       case 4:
//                           Completion completion = completionService.findByExerciseInfoId(composePaper.getExerinfoid());
//                           ExamBlankrecord examBlankrecord=
//                   }
//             }
//            }
//
//        }catch(Exception e){
//        e.printStackTrace();
//        return R.error();
//        }
//   }
}